package com.yuji.aacar.base;

import android.content.Context;
import android.text.TextUtils;

import com.yuji.aacar.bean.beans.Order;
import com.yuji.aacar.bean.beans.PushMessage;
import com.yuji.aacar.bean.beans.QueryCarAddressInfo;
import com.yuji.aacar.bean.beans.Serial;
import com.yuji.aacar.bean.response.LoginResponse;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.utils.CorePersistenceUtil;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.tools.Tools;

import java.util.ArrayList;


public class UserCenter {

    public static boolean isLogin() {
        return (boolean) CorePersistenceUtil.getParam(Const.LOGIN_KEY, false);
    }

    public static void login() {
        CorePersistenceUtil.setParam(Const.LOGIN_KEY, true);
    }

    public static void saveCurrentUser(LoginResponse user) {
        Tools.saveToken( user.getToken(), user.getMemberId());
        CorePersistenceUtil.saveObject(Const.USER_KEY,user);
    }

    public static LoginResponse getCurrentUser() {
        LoginResponse user = (LoginResponse) CorePersistenceUtil.getObject(Const.USER_KEY);
        if (null == user || TextUtils.isEmpty(user.getPhoneNo())) {
            CoreEventCenter.postMessage(EventConst.LOGIN_OUT, "重新登录");
        }
        return user;
    }

    public static void logout(Context context) {
        CorePersistenceUtil.setParam(Const.LOGIN_KEY, false);
        CorePersistenceUtil.setParam(Const.TOKEN_KEY, "");
        CorePersistenceUtil.saveObject(Const.USER_KEY,new LoginResponse());

    }

    public static boolean isFirstUse(String userId) {
        return (boolean) CorePersistenceUtil.getParam(userId+Const.IS_FIRST_USE, true);
    }

    public static void setIsFirstUse(String userId) {
        CorePersistenceUtil.setParam(userId+Const.IS_FIRST_USE, false);
    }

    public static boolean isFirstClickImmediateWash(String userId) {
        return (boolean) CorePersistenceUtil.getParam(userId+Const.IS_FIRST_CLICK_IMMEDIATE, true);
    }

    public static void setFirstClickImmediateWash(String userId) {
        CorePersistenceUtil.setParam(userId+Const.IS_FIRST_CLICK_IMMEDIATE, false);
    }

    public static boolean isFirstClickReserveWash(String userId) {
        return (boolean) CorePersistenceUtil.getParam(userId+Const.IS_FIRST_CLICK_RESERVE, true);
    }

    public static void setFirstClickReserveWash(String userId) {
        CorePersistenceUtil.setParam(userId+Const.IS_FIRST_CLICK_RESERVE, false);
    }


    public static void setOrderHistory(String userId, Order order) {
        CorePersistenceUtil.saveObject(userId + Const.ORDER_HISTORY, order);
    }

    public static Order getOrderHistory(String userId) {
        return (Order) CorePersistenceUtil.getObject(userId + Const.ORDER_HISTORY);
    }

    public static void setFirstCar(String userId, Serial car) {
        CorePersistenceUtil.saveObject(userId + Const.FIRST_CAR, car);
        LogUtil.e("--setFirstCar--setFirstCar--", UserCenter.getFirstCar(UserCenter.getCurrentUser().getMemberId()));
    }

    public static Serial getFirstCar(String userId) {
        return (Serial) CorePersistenceUtil.getObject(userId + Const.FIRST_CAR);
    }

    public static void setTags(String userId, String tag) {
        CorePersistenceUtil.setParam(userId + Const.ADDRESS_TAG, tag);
    }

    public static String getTags(String userId) {
        return (String) CorePersistenceUtil.getParam(userId + Const.ADDRESS_TAG, "");
    }

    public static void setDefaultAddress(String userId, QueryCarAddressInfo tag) {
        CorePersistenceUtil.saveObject(userId + Const.ADDRESS_DEFAULT, tag);
    }

    public static QueryCarAddressInfo getDefaultAddress(String userId) {
        return (QueryCarAddressInfo) CorePersistenceUtil.getObject(userId + Const.ADDRESS_DEFAULT);
    }

    public static void savePushMessage (String userId, ArrayList<PushMessage> message) {
        CorePersistenceUtil.saveObject(userId + Const.PUSH_MESSAGE, message);
    }

    public static ArrayList<PushMessage> getPushMessages(String userId) {
        return (ArrayList<PushMessage>) CorePersistenceUtil.getObject(userId + Const.PUSH_MESSAGE);
    }

    public static void setNewMessage(String userId, String has) {
        CorePersistenceUtil.setParam(userId + Const.PUSH_MESSAGE_HAS, has);
    }

    public static String getNewMessage(String userId) {
        return (String) CorePersistenceUtil.getParam(userId + Const.PUSH_MESSAGE_HAS,"0");
    }

    public static void setDeviceToken(String deviceToken) {
        CorePersistenceUtil.setParam( Const.DEVICE_TOKEN, deviceToken);
    }

    public static String getDeviceTokens() {
        return (String) CorePersistenceUtil.getParam(Const.DEVICE_TOKEN,"0");
    }
}
